﻿using System;
using System.Collections;
using System.Data;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.DAL;
using Telerik.Web.UI;
using System.Web.UI;


namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_MAS_FISCAL_Period : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        M_ACM_MAS_FISCAL_Period m_fiscal_period = new M_ACM_MAS_FISCAL_Period();
        D_ACM_MAS_FISCAL_Period d_fiscal_period = new D_ACM_MAS_FISCAL_Period();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
                //Session["UserID"] = "555";
                grdFiscalPeriod.DataSource = d_fiscal_period.GetList();
                grdFiscalPeriod.DataBind();
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            UpdateUI1("Insert");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                UpdateUI1("Save");
                //m_fiscal_period.fiscal_period = Convert.ToInt32(hfiscal_period_seq.Text);
                if (hStatus.Text == "Insert")
                {
                    m_fiscal_period.fiscal_period_code = txtfiscal_period_code.Text;
                    m_fiscal_period.fiscal_period_name = txtfiscal_period_name.Text;
                    m_fiscal_period.fiscal_period_remark = txtfiscal_period_remark.Text;
                    m_fiscal_period.created_by = Convert.ToInt32(main.strEmpID);
                    m_fiscal_period.created_when = DateTime.Now;
                    m_fiscal_period.record_status = "U";
                    m_fiscal_period.fiscal_start_date = dtpStartDate.GetDateValue().Value;
                    m_fiscal_period.fiscal_end_date = dtpEndDate.GetDateValue().Value;
                    d_fiscal_period.Insert(m_fiscal_period);
                }
                else if (hStatus.Text == "Update")
                {
                    m_fiscal_period.fiscal_period = Convert.ToInt32(hfiscal_period_seq.Text);
                    m_fiscal_period.fiscal_period_code = txtfiscal_period_code.Text;
                    m_fiscal_period.fiscal_period_name = txtfiscal_period_name.Text;
                    m_fiscal_period.fiscal_period_remark = txtfiscal_period_remark.Text;
                    m_fiscal_period.updated_by = Convert.ToInt32(main.strEmpID);
                    m_fiscal_period.updated_when = DateTime.Now;
                    m_fiscal_period.record_status = "U";
                    m_fiscal_period.fiscal_start_date = dtpStartDate.GetDateValue().Value;
                    m_fiscal_period.fiscal_end_date = dtpEndDate.GetDateValue().Value;
                    d_fiscal_period.Update(m_fiscal_period);
                }
                grdFiscalPeriod.DataSource = d_fiscal_period.GetList();
                grdFiscalPeriod.DataBind();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
        }
        protected void grdFiscalPeriod_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                grdFiscalPeriod.DataSource = d_fiscal_period.GetList();
                grdFiscalPeriod.DataBind();
            }
        }
        protected void grdFiscalPeriod_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DataEdit")
                {
                    UpdateUI1("Update");
                    M_ACM_MAS_FISCAL_Period info = d_fiscal_period.GetListFillBySeq(main.ConvertNullInt(grdFiscalPeriod.Items[e.Item.ItemIndex]["fiscal_period"].Text));
                    hfiscal_period_seq.Text = info.fiscal_period.ToString();
                    txtfiscal_period_code.Text = info.fiscal_period_code;
                    txtfiscal_period_name.Text = info.fiscal_period_name;
                    txtfiscal_period_remark.Text = info.fiscal_period_remark;
                    dtpStartDate.SetValue(info.fiscal_start_date);
                    dtpEndDate.SetValue(info.fiscal_end_date);
                }
                else if (e.CommandName == "Delete")
                {
                    M_ACM_MAS_FISCAL_Period info = d_fiscal_period.GetListFillBySeq(main.ConvertNullInt(grdFiscalPeriod.Items[e.Item.ItemIndex]["fiscal_period"].Text));
                    m_fiscal_period.fiscal_period = Convert.ToInt32(grdFiscalPeriod.Items[e.Item.ItemIndex]["fiscal_period"].Text);
                    m_fiscal_period.fiscal_period_code = info.fiscal_period_code;//txtfiscal_period_code.Text;
                    m_fiscal_period.fiscal_period_name = info.fiscal_period_name;//txtfiscal_period_name.Text;
                    m_fiscal_period.fiscal_period_remark = info.fiscal_period_remark;//txtfiscal_period_remark.Text;
                    m_fiscal_period.updated_by = Convert.ToInt32(main.strEmpID);
                    m_fiscal_period.updated_when = DateTime.Now;
                    m_fiscal_period.record_status = "D";
                    m_fiscal_period.fiscal_start_date = info.fiscal_start_date;//dtpStartDate.GetDateValue();
                    m_fiscal_period.fiscal_end_date = info.fiscal_end_date;//dtpEndDate.GetDateValue();
                    d_fiscal_period.Update(m_fiscal_period);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                grdFiscalPeriod.DataSource = d_fiscal_period.GetList();
                grdFiscalPeriod.DataBind();
            }
        }
        
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    setObj(false);
                    ClearObj();
                    break;
                case "Insert" :
                    lblStatus.Text = "เพิ่มข้อมูล";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    setObj(true);
                    ClearObj();
                    txtfiscal_period_code.Focus();
                    hStatus.Text = "Insert";
                    break;
                case "Update":
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    setObj(true);
                    txtfiscal_period_code.Focus();
                    hStatus.Text = "Update";
                    break;
                case "Save":
                    main.MessageAlert(Page, "บันทึกข้อมูลเรียบร้อยแล้ว");
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    setObj(false);
                    break;
                case "Cancel":
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnInsert.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    setObj(false);
                    ClearObj();
                    break;
            }
        }
        protected void setObj(Boolean pFlag)
        {
            txtfiscal_period_code.Enabled = pFlag;
            txtfiscal_period_name.Enabled = pFlag;
            txtfiscal_period_remark.Enabled = pFlag;
        }
        protected void ClearObj()
        {
            txtfiscal_period_code.Text = "";
            txtfiscal_period_name.Text = "";
            txtfiscal_period_remark.Text = "";
        }
        protected Boolean checkValidate()
        {
            Boolean pFlag = true;
            if (txtfiscal_period_code.Text == "")
            {
                lblStatus.Text = "กรุณากรอกข้อมูลรหัสงวดเดือนด้วย";
                pFlag = false;
                txtfiscal_period_code.Focus();
                main.MessageAlert(Page, "กรุณากรอกข้อมูลรหัสงวดเดือนด้วย");
            }
            else if (txtfiscal_period_name.Text == "")
            {
                lblStatus.Text = "กรุณากรอกชื่อของงวดด้วย";
                pFlag = false;
                txtfiscal_period_name.Focus();
                main.MessageAlert(Page, "กรุณากรอกชื่อของงวดด้วย");
            }
            else if (txtfiscal_period_code.Text.Length > 20)
            {
                lblStatus.Text = "รหัสงวดเกิน 20 ตัวอักษร";
                pFlag = false;
                txtfiscal_period_code.Focus();
                main.MessageAlert(Page, "รหัสงวดเกิน 20 ตัวอักษร");
            }
            else if (txtfiscal_period_name.Text.Length > 255)
            {
                lblStatus.Text = "ชื่องวดเกิน 255 ตัวอักษร";
                pFlag = false;
                txtfiscal_period_name.Focus();
                main.MessageAlert(Page, "ชื่องวดเกิน 255 ตัวอักษร");
            }
            else if (txtfiscal_period_remark.Text.Length > 255)
            {
                lblStatus.Text = "หมายเหตุกรอกเกิน 255 ตัวอักษร";
                pFlag = false;
                txtfiscal_period_remark.Focus();
                main.MessageAlert(Page, "หมายเหตุกรอกเกิน 255 ตัวอักษร");
            }
            else if (!dtpStartDate.GetDateValue().HasValue)
            {
                lblStatus.Text = "กรุณากรอกวันที่เริ่มต้น";
                pFlag = false;
                txtfiscal_period_remark.Focus();
                main.MessageAlert(Page, "กรุณากรอกวันที่เริ่มต้น");
            }
            else if (!dtpEndDate.GetDateValue().HasValue)
            {
                lblStatus.Text = "กรุณากรอกวันที่สิ้นสุด";
                pFlag = false;
                txtfiscal_period_remark.Focus();
                main.MessageAlert(Page, "กรุณากรอกวันที่สิ้นสุด");
            }
            return pFlag;
        }
        
    }
}
